/**
 * 
 */
package com.stulsoft.yssmugmugutils;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Comparator;
import java.util.SortedSet;
import java.util.TreeSet;

/**
 * Container for category.
 * 
 * @author Yuriy
 *
 */
public class YSCategory implements Comparable<YSCategory>, Comparator<YSCategory> {
	private final int id;
	private final String name;
	private final SortedSet<YSCategory> subcategories;
	private final SortedSet<String> albums;

	/**
	 * Creates a new instance of the YSCategory class.
	 *  
	 * @param id -  the category ID.
	 * @param name - the category name.
	 */
	public YSCategory(int id, String name) {
		if (name == null || name.length() == 0) {
			throw new NullPointerException("name is null or empty.");
		}

		this.id = id;
		this.name = name;

		this.subcategories = new TreeSet<YSCategory>();
		this.albums = new TreeSet<String>();
	}

	/**
	 * Gets the category ID.
	 * 
	 * @return the category ID.
	 */
	public int getId() {
		return id;
	}

	/**
	 * Gets the category name.
	 * 
	 * @return the category name.
	 */
	public String getName() {
		return name;
	}

	/**
	 * Gets the collection of the sub-categories.
	 * 
	 * @return the collection of the sub-categories.
	 */
	public Collection<YSCategory> getSubcategories() {
		return new ArrayList<YSCategory>(subcategories);
	}
	
	/**
	 * Gets the sub-category for specified ID.
	 * 
	 * @param id - specifies the sub-category ID.
	 * @return the the sub-category for specified ID, if exists; null - otherwise.
	 */
	public YSCategory getSubcategory(int id){
		for(YSCategory subcategory : subcategories){
			if (subcategory.getId() == id){
				return subcategory;
			}
		}
		
		return null;
	}

	/**
	 * Adds new sub-category to collection of sub-categories.
	 * 
	 * @param subcategory - the sub-category to add.
	 */
	public void addSubcategory(YSCategory subcategory) {
		if (subcategory == null) {
			throw new NullPointerException("subcategory is null.");
		}
		if (!subcategories.contains(subcategory)) {
			subcategories.add(subcategory);
		}
	}

	/**
	 * Gets the collection of albums.
	 * 
	 * @return the collection of albums.
	 */
	public Collection<String> getAlbums() {
		return new ArrayList<String>(albums);
	}

	/**
	 * Adds new album to collection of albums.
	 * 
	 * @param album - the album to add.
	 */
	public void addAlbum(String album) {
		if (album == null || album.length() == 0) {
			throw new NullPointerException("album is null or empty.");
		}
		if (!albums.contains(album)) {
			albums.add(album);
		}
	}

	/**
	 * Compares with another category.
	 * 
	 * Two categories are equal if they have same ID and name.
	 * 
	 * @see java.lang.Object#equals(java.lang.Object)
	 */
	@Override
	public boolean equals(Object obj) {
		if (obj == null)
			return false;
		if (obj == this)
			return true;
		if (obj.getClass() != getClass())
			return false;

		YSCategory theCategory = (YSCategory) obj;

		return ((theCategory.getId() == getId()) && (getName()
				.equals(theCategory.getName())));
	}

	/**
	 * Returns the hash code.
	 * 
	 * Hash code is the category ID.
	 * 
	 * @see java.lang.Object#hashCode()
	 */
	@Override
	public int hashCode() {
		return getId();
	}

	@Override
	public int compareTo(YSCategory o) {
		return getName().compareTo(o.getName());
	}

	@Override
	public int compare(YSCategory o1, YSCategory o2) {
		return o1.compareTo(o2);
	}
}
